package com.example.controller;

import com.example.pojo.dao.ChatDao;
import com.example.pojo.entity.Chat;
import com.example.service.ChatService;
import com.example.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 用来创建一对一的聊天表数据
 * 并不需要返回数据
 * @author ..
 * @Date 2023/10/28 13:54
 */
@Component
public class WebSocketController {
    private Integer jsId;
    private Integer jrId;

    @Autowired
    private ChatDao chatDao;


    private static ChatService chatService;

    @Autowired
    private void cs(ChatService chatService){
        WebSocketController.chatService=chatService;
    }


    private static MessageService messageService;

    @Autowired
    private void ms(MessageService messageService){
        WebSocketController.messageService=messageService;
    }


    //建立连接时调用
    public void addWS(Integer jsId,Integer jrId,String message,Integer sige,Integer posId){
        Chat chat = chatService.findByJsJrID(jsId,jrId);
        if (chat==null){
            chatService.insertJsJr(jrId,jsId);
            System.out.println("posId===>");
            System.out.println(posId);
            if (posId!=null){
                List<Integer> posIds = messageService.messageById(jsId);
                boolean a = true;
                for (Integer p : posIds) {
                    if (posId == p) {
                        a = false;
                    }
                }
                if (a) {
                    messageService.insertMes(jsId, jrId, posId);
                }
            }
            chat = chatService.findByJsJrID(jsId,jrId);
        }
        addCR(chat.getId(),sige,message);
    }

    public boolean addCR(Integer chatId,Integer sige,String message){
        chatService.addCR(chatId,sige,message);
        return false;
    }
}
